package com.inforich.app.api.controller.orderManage;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inforich.app.api.dto.BookingAllocationDTO;
import com.inforich.app.api.dto.BookingDTO;
import com.inforich.app.api.service.IAppBookingService;
import com.inforich.app.component.LoginUser;
import com.inforich.app.component.LoginUserApp;
import com.inforich.app.component.PreAppRole;
import com.inforich.appManage.service.IContactService;
import com.inforich.common.annotation.RepeatSubmit;
import com.inforich.common.core.controller.AppBaseController;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.orderManage.domain.Booking;
import com.inforich.orderManage.service.IBookingService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 预约单管理
 * @author: HJM
 * @date: 2022/10/31
 */
@RestController
@RequestMapping("/h5api/orderManage/booking")
public class AppBookingController extends AppBaseController {

  @Autowired
  private IAppBookingService appBookingService;
  @Autowired
  private IBookingService bookingService;
  @Autowired
  private IContactService contactService;

  /**
   * 我的预约单(分页列表)
   */
  @PreAppRole("project_admin")
  @GetMapping("/myBookingList")
  public Page<Booking> list(@LoginUser LoginUserApp loginUserApp, Page<Booking> page) {
    String projectId = contactService.getProjectIdByAppUserId(loginUserApp.getId());
    if (StrUtil.isBlank(projectId)) {
      return page;
    }
    Map<String, Object> map = new HashMap<>();
    map.put("projectId", projectId);
    bookingService.pageBooking(page, map);
    return page;
  }

  /**
   * 我的运输单(最新)
   */
  @PreAppRole("project_admin")
  @GetMapping("/myLastBookingList")
  public AjaxResult myLastBookingList(@LoginUser LoginUserApp loginUserApp) {
    String projectId = contactService.getProjectIdByAppUserId(loginUserApp.getId());
    if (StrUtil.isBlank(projectId)) {
      return AjaxResult.error("请先进行项目认证");
    }
    Map<String, Object> map = new HashMap<>();
    map.put("projectId", projectId);
    List<Booking> bookingList = bookingService.listLastBooking(map);
    if (CollUtil.isEmpty(bookingList)) {
      return AjaxResult.success(Lists.newArrayList());
    }
    List<Booking> resultList = bookingList.stream().limit(2).collect(Collectors.toList());
    return AjaxResult.success(resultList);
  }

  /**
   * 我的最新一次预约单
   */
  @PreAppRole("project_admin")
  @GetMapping("/myLastBookingDetail")
  public AjaxResult myLastBookingDetail(@LoginUser LoginUserApp loginUserApp) {
    String projectId = contactService.getProjectIdByAppUserId(loginUserApp.getId());
    if (StrUtil.isBlank(projectId)) {
      return AjaxResult.success(new Booking());
    }
    Booking booking = bookingService.getLastBookingByProjectId(projectId);
    return AjaxResult.success(booking == null ? new Booking() : booking);
  }

  /**
   * 查询详情
   */
  @GetMapping("/query")
  public AjaxResult getInfo(String id) {
    return AjaxResult.success(appBookingService.getInfo(id));
  }

  /**
   * 新增预约单
   */
  @PreAppRole("project_admin")
  @PostMapping("/add")
  @RepeatSubmit
  public AjaxResult addBooking(@LoginUser LoginUserApp loginUserApp,
      @Validated @RequestBody BookingDTO bookingDTO) {
    return appBookingService.addBooking(loginUserApp, bookingDTO);
  }

  /**
   * 公司运输单(最新)
   */
  @PreAppRole("transport_company_admin")
  @GetMapping("/getCompanyLastBookingList")
  public AjaxResult getCompanyLastBookingList(@LoginUser LoginUserApp loginUserApp) {
    // 公司ID
    String transportCompanyId = contactService.getTransportCompanyIdByAppUserId(
        loginUserApp.getId());
    if (StrUtil.isBlank(transportCompanyId)) {
      return AjaxResult.error("缺少公司信息");
    }
    Map<String, Object> map = new HashMap<>();
    map.put("transportCompanyId", transportCompanyId);
    List<Booking> bookingList = bookingService.listLastBooking(map);
    if (CollUtil.isEmpty(bookingList)) {
      return AjaxResult.success(Lists.newArrayList());
    }
    List<Booking> resultList = bookingList.stream().limit(2).collect(Collectors.toList());
    return AjaxResult.success(resultList);
  }

  /**
   * 公司预约单(分页列表)
   */
  @PreAppRole("transport_company_admin")
  @GetMapping("/getCompanyBookingList")
  public Page<Booking> getCompanyBookingList(@LoginUser LoginUserApp loginUserApp,
      Page<Booking> page, Booking booking) {
    // 公司ID
    String transportCompanyId = contactService.getTransportCompanyIdByAppUserId(
        loginUserApp.getId());
    if (StrUtil.isBlank(transportCompanyId)) {
      return page;
    }
    Map<String, Object> map = beanToMap(booking);
    map.put("transportCompanyId", transportCompanyId);
    // 标识是公司(不查询状态为草稿的状态)
    map.put("isTransportCompany", "1");
    bookingService.pageBooking(page, map);
    return page;
  }

  /**
   * 分配车辆
   */
  @PreAppRole("transport_company_admin")
  @PostMapping("/allocationBooking")
  public AjaxResult allocationBooking(@LoginUser LoginUserApp loginUserApp,
      @Validated @RequestBody BookingAllocationDTO dto) {
    return appBookingService.allocationBooking(loginUserApp, dto);
  }

  /**
   * 司机预约单(最新)
   */
  @PreAppRole("driver")
  @GetMapping("/getDriverLastBookingList")
  public AjaxResult getDriverLastBookingList(@LoginUser LoginUserApp loginUserApp) {
    // 司机ID
    Long driverId = contactService.getDriverIdByAppUserId(loginUserApp.getId());
    Map<String, Object> map = new HashMap<>();
    map.put("driverId", driverId);
    List<Booking> bookingList = bookingService.listLastBooking(map);
    if (CollUtil.isEmpty(bookingList)) {
      return AjaxResult.success(Lists.newArrayList());
    }
    List<Booking> resultList = bookingList.stream().limit(2).collect(Collectors.toList());
    return AjaxResult.success(resultList);
  }

  /**
   * 司机预约单(分页列表)
   */
  @PreAppRole("driver")
  @GetMapping("/getDriverBookingList")
  public Page<Booking> getDriverBookingList(@LoginUser LoginUserApp loginUserApp,
      Page<Booking> page, Booking booking) {
    // 司机ID
    Long driverId = contactService.getDriverIdByAppUserId(loginUserApp.getId());
    if (driverId == null) {
      return page;
    }
    Map<String, Object> map = beanToMap(booking);
    map.put("driverId", driverId);
    bookingService.pageBooking(page, map);
    return page;
  }

  /**
   * [司机]我的预约单
   */
  @PreAppRole("driver")
  @GetMapping("/queryMyBookingInfo")
  public AjaxResult queryMyBookingInfo(@LoginUser LoginUserApp loginUserApp, String bookingId) {
    return AjaxResult.success(appBookingService.queryMyBookingInfo(loginUserApp, bookingId));
  }
}
